import 'package:cult/function/utils/my_common.dart';
import 'package:flutter/material.dart';
import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:animated_custom_dropdown/custom_dropdown.dart';
import 'growth_tracking_controller.dart';

/// 植物成长记录页面
/// 使用GetX进行页面逻辑分离
class GrowthTrackingPage extends GetView<GrowthTrackingController> {
  const GrowthTrackingPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: const BoxDecoration(
          image: DecorationImage(
            image: AssetImage('assets/images/home_bg.png'),
            fit: BoxFit.fill,
          ),
        ),
        child: SafeArea(
          bottom: false,
          child: Column(
            children: [
              // 顶部导航栏
              _buildTopNavigation(context),

              // 页面内容
              Expanded(
                child: SingleChildScrollView(
                  child: Column(
                    children: [
                      // 本周日期表
                      _buildWeeklyCalendar(context),

                      SizedBox(height: 20.h),

                      // 记录表单
                      _buildRecordForm(context),

                      SizedBox(height: 20.h),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  /// 构建顶部导航栏
  Widget _buildTopNavigation(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(left: 16.w, right: 16.w, top: 0.h, bottom: 16.h),
      child: Row(
        children: [
          // 返回按钮
          Icon(
            Icons.arrow_back_ios_new,
            size: 20.sp,
            color: MyCommonUtils.white,
          ).padding(all: 10.w).inkWell(onTap: () => Get.back()),

          SizedBox(width: 16.w),

          // 标题
          Expanded(
            child: Text(
              'Growth Tracking',
              style: TextStyle(
                fontSize: 24.sp,
                fontFamily:
                    GoogleFonts.berkshireSwash(
                      fontWeight: FontWeight.w400,
                    ).fontFamily,
                color: MyCommonUtils.white,
              ),
              textAlign: TextAlign.center,
            ),
          ),

          // 占位空间，保持标题居中
          SizedBox(width: 56.w),
        ],
      ),
    );
  }

  /// 构建本周日期表
  Widget _buildWeeklyCalendar(BuildContext context) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 16.w),
      child: AspectRatio(
        aspectRatio: 358 / 134, // 恢复原始高度比例
        child: Container(
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage('assets/images/date_bg.png'),
              fit: BoxFit.fill,
            ),
            borderRadius: BorderRadius.circular(12.r),
            border: Border.all(color: MyCommonUtils.orange, width: 1.w),
          ),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              // 月份和年份
              Text(
                controller.getCurrentMonthYear(),
                style: TextStyle(
                  fontSize: 20.sp,
                  fontWeight: FontWeight.w700,
                  color: MyCommonUtils.white,
                ),
              ),

              // 星期标题和日期列表
              Obx(() {
                final weekDays = controller.getCurrentWeekDays();
                final weekLabels = [
                  'Mon',
                  'Tue',
                  'Wed',
                  'Thu',
                  'Fri',
                  'Sat',
                  'Sun',
                ];

                return Column(
                  children: [
                    // 星期标题
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children:
                          weekLabels
                              .map(
                                (label) => Expanded(
                                  child: Text(
                                    label,
                                    style: TextStyle(
                                      fontSize: 14.sp,
                                      color: MyCommonUtils.white,
                                      fontWeight: FontWeight.w400,
                                      fontFamily:
                                          GoogleFonts.berkshireSwash(
                                            fontWeight: FontWeight.w400,
                                          ).fontFamily,
                                    ),
                                    textAlign: TextAlign.center,
                                  ),
                                ),
                              )
                              .toList(),
                    ),

                    SizedBox(height: 4.h), // 进一步减少间距
                    // 日期列表
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children:
                          weekDays.map((day) {
                            final isSelected =
                                controller.selectedDate.value.day == day.day;
                            final hasRecord = controller.hasRecordForDate(day);
                            final isToday = _isToday(day);
                            final isPast = _isPast(day);
                            final isFuture = _isFuture(day);

                            return Expanded(
                              child: GestureDetector(
                                onTap: () {
                                  // 只有今天可以编辑，其他日期只能查看
                                  controller.selectDate(day);
                                },
                                child: Column(
                                  children: [
                                    // 状态图标
                                    _buildDateIndicator(
                                      day,
                                      isSelected,
                                      hasRecord,
                                      isToday,
                                      isPast,
                                      isFuture,
                                    ),
                                  ],
                                ),
                              ),
                            );
                          }).toList(),
                    ),
                  ],
                );
              }),
            ],
          ),
        ),
      ),
    );
  }

  /// 判断是否是今天
  bool _isToday(DateTime day) {
    final now = DateTime.now();
    return day.year == now.year && day.month == now.month && day.day == now.day;
  }

  /// 判断是否是过去的日期
  bool _isPast(DateTime day) {
    final now = DateTime.now();
    final today = DateTime(now.year, now.month, now.day);
    final targetDay = DateTime(day.year, day.month, day.day);
    return targetDay.isBefore(today);
  }

  /// 判断是否是未来的日期
  bool _isFuture(DateTime day) {
    final now = DateTime.now();
    final today = DateTime(now.year, now.month, now.day);
    final targetDay = DateTime(day.year, day.month, day.day);
    return targetDay.isAfter(today);
  }

  /// 构建日期指示器
  Widget _buildDateIndicator(
    DateTime day,
    bool isSelected,
    bool hasRecord,
    bool isToday,
    bool isPast,
    bool isFuture,
  ) {
    // 根据设计图的状态显示逻辑
    if (isToday) {
      // 今天：大圆 42.w * 42.w 橙色圆圈，白色日期数字
      return Container(
        width: 42.w,
        height: 42.w,
        decoration: BoxDecoration(
          color: MyCommonUtils.orange,
          shape: BoxShape.circle,
        ),
        child: Center(
          child: Text(
            day.day.toString(),
            style: TextStyle(
              fontSize: 16.sp,
              color: MyCommonUtils.white,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
      );
    } else if (isPast) {
      // 过去的日期：小圆 24.w * 24.w
      if (hasRecord) {
        // 有记录：橙色圆圈，白色勾选图标，圆圈内部下方有白点
        return Stack(
          children: [
            Container(
              width: 24.w,
              height: 24.w,
              decoration: BoxDecoration(
                color: MyCommonUtils.orange,
                shape: BoxShape.circle,
              ),
              child: Icon(Icons.check, color: MyCommonUtils.white, size: 12.sp),
            ),
            // 白点放在圆圈内部下方
            Positioned(
              bottom: -10.h,
              left: 0,
              right: 0,
              child: Center(
                child: Container(
                  width: 4.w,
                  height: 4.w,
                  decoration: BoxDecoration(
                    color: MyCommonUtils.white,
                    shape: BoxShape.circle,
                  ),
                ),
              ),
            ),
          ],
        );
      } else {
        // 无记录：黑色圆圈，白色X图标，圆圈内部下方有白点
        return Stack(
          clipBehavior: Clip.none,
          children: [
            Container(
              width: 24.w,
              height: 24.w,
              decoration: BoxDecoration(
                color: MyCommonUtils.black,
                shape: BoxShape.circle,
              ),
              child: Icon(Icons.close, color: MyCommonUtils.white, size: 12.sp),
            ),
            // 白点放在圆圈内部下方
            Positioned(
              bottom: -10.h,
              left: 0,
              right: 0,
              child: Center(
                child: Container(
                  width: 4.w,
                  height: 4.w,
                  decoration: BoxDecoration(
                    color: MyCommonUtils.white,
                    shape: BoxShape.circle,
                  ),
                ),
              ),
            ),
          ],
        );
      }
    } else {
      // 未来的日期：小圆 24.w * 24.w 白色圆圈，深灰色日期数字，无白点
      return Container(
        width: 24.w,
        height: 24.w,
        decoration: BoxDecoration(
          color: MyCommonUtils.white,
          shape: BoxShape.circle,
        ),
        child: Center(
          child: Text(
            day.day.toString(),
            style: TextStyle(
              fontSize: 12.sp,
              color: MyCommonUtils.black,
              fontWeight: FontWeight.w500,
            ),
          ),
        ),
      );
    }
  }

  /// 构建未打卡显示界面
  Widget _buildNoRecordDisplay(BuildContext context, DateTime selectedDate) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 16.w),
      padding: EdgeInsets.all(20.w),
      decoration: BoxDecoration(
        color: MyCommonUtils.green.withValues(alpha: 0.8),
        borderRadius: BorderRadius.circular(12.r),
        border: Border.all(color: MyCommonUtils.orange, width: 1.w),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          // 未打卡图标
          Container(
            width: 80.w,
            height: 80.w,
            decoration: BoxDecoration(
              color: MyCommonUtils.orange,
              shape: BoxShape.circle,
            ),
            child: Icon(Icons.close, color: MyCommonUtils.white, size: 40.sp),
          ),

          SizedBox(height: 24.h),

          // 未打卡标题
          Text(
            'No Record',
            style: TextStyle(
              fontSize: 24.sp,
              fontWeight: FontWeight.bold,
              color: MyCommonUtils.white,
              fontFamily:
                  GoogleFonts.berkshireSwash(
                    fontWeight: FontWeight.w400,
                  ).fontFamily,
            ),
          ),

          SizedBox(height: 12.h),

          // 未打卡描述
          Text(
            'You didn\'t record your plant growth on ${_formatDate(selectedDate)}',
            style: TextStyle(
              fontSize: 16.sp,
              color: MyCommonUtils.white.withValues(alpha: 0.8),
              height: 1.4,
            ),
            textAlign: TextAlign.center,
          ),

          SizedBox(height: 20.h),

          // 提示信息
          Container(
            padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 12.h),
            decoration: BoxDecoration(
              color: MyCommonUtils.white.withValues(alpha: 0.2),
              borderRadius: BorderRadius.circular(8.r),
            ),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Icon(
                  Icons.info_outline,
                  color: MyCommonUtils.white,
                  size: 20.sp,
                ),
                SizedBox(width: 8.w),
                Text(
                  'You can only edit today\'s record.',
                  style: TextStyle(fontSize: 14.sp, color: MyCommonUtils.white),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  /// 构建未来日期显示界面
  Widget _buildFutureDateDisplay(BuildContext context, DateTime selectedDate) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 16.w),
      padding: EdgeInsets.all(20.w),
      decoration: BoxDecoration(
        color: MyCommonUtils.green.withValues(alpha: 0.8),
        borderRadius: BorderRadius.circular(12.r),
        border: Border.all(color: MyCommonUtils.orange, width: 1.w),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          // 未来日期图标
          Container(
            width: 80.w,
            height: 80.w,
            decoration: BoxDecoration(
              color: MyCommonUtils.white,
              shape: BoxShape.circle,
            ),
            child: Icon(
              Icons.schedule,
              color: MyCommonUtils.orange,
              size: 40.sp,
            ),
          ),

          SizedBox(height: 24.h),

          // 未来日期标题
          Text(
            'Future Date',
            style: TextStyle(
              fontSize: 24.sp,
              fontWeight: FontWeight.bold,
              color: MyCommonUtils.white,
              fontFamily:
                  GoogleFonts.berkshireSwash(
                    fontWeight: FontWeight.w400,
                  ).fontFamily,
            ),
          ),

          SizedBox(height: 12.h),

          // 未来日期描述
          Text(
            'You can\'t record plant growth for ${_formatDate(selectedDate)} yet',
            style: TextStyle(
              fontSize: 16.sp,
              color: MyCommonUtils.white.withValues(alpha: 0.8),
              height: 1.4,
            ),
            textAlign: TextAlign.center,
          ),

          SizedBox(height: 20.h),

          // 提示信息
          Container(
            padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 12.h),
            decoration: BoxDecoration(
              color: MyCommonUtils.white.withValues(alpha: 0.2),
              borderRadius: BorderRadius.circular(8.r),
            ),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Icon(
                  Icons.info_outline,
                  color: MyCommonUtils.white,
                  size: 20.sp,
                ),
                SizedBox(width: 8.w),
                Text(
                  'You can only record today\'s growth.',
                  style: TextStyle(fontSize: 14.sp, color: MyCommonUtils.white),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  /// 格式化日期显示
  String _formatDate(DateTime date) {
    final months = [
      'Jan',
      'Feb',
      'Mar',
      'Apr',
      'May',
      'Jun',
      'Jul',
      'Aug',
      'Sep',
      'Oct',
      'Nov',
      'Dec',
    ];
    return '${months[date.month - 1]} ${date.day}';
  }

  /// 构建记录表单
  Widget _buildRecordForm(BuildContext context) {
    return Obx(() {
      final canEdit = controller.canEditSelectedDate();
      final selectedDate = controller.selectedDate.value;
      final hasRecord = controller.hasRecordForDate(selectedDate);
      final isPast = _isPast(selectedDate);
      final isFuture = _isFuture(selectedDate);
      final isNoRecord = isPast && !hasRecord;

      print(
        'canEdit: $canEdit, selectedDate: $selectedDate, isPast: $isPast, isFuture: $isFuture',
      );

      // 如果是过去的日期且没有记录，显示未打卡提示
      if (isNoRecord) {
        return _buildNoRecordDisplay(context, selectedDate);
      }

      // 如果是未来的日期，显示未来日期提示
      if (isFuture) {
        return _buildFutureDateDisplay(context, selectedDate);
      }

      return Container(
        margin: EdgeInsets.symmetric(horizontal: 16.w),
        padding: EdgeInsets.all(20.w),
        decoration: BoxDecoration(
          color: MyCommonUtils.green.withValues(alpha: 0.8),
          borderRadius: BorderRadius.circular(12.r),
          border: Border.all(color: MyCommonUtils.orange, width: 1.w),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 记录标题
            Center(
              child: Text(
                canEdit ? 'Record' : 'View Record',
                style: TextStyle(
                  fontSize: 18.sp,
                  fontWeight: FontWeight.bold,
                  color: MyCommonUtils.white,
                ),
              ),
            ),

            SizedBox(height: 20.h),

            // 植物图片
            _buildPlantImagesSection(context, canEdit),

            SizedBox(height: 16.h),

            // 植物高度
            _buildInputField(
              'Plant height',
              'Please enter your plant height.',
              controller.plantHeightController,
              isNumber: true,
              readOnly: !canEdit,
            ),

            SizedBox(height: 16.h),

            // 叶片数量
            _buildBladesSection(context),

            SizedBox(height: 16.h),

            // 浇水信息
            _buildWateringSection(context, canEdit: canEdit),

            SizedBox(height: 16.h),

            // 施肥信息
            _buildFertilizationSection(context, canEdit: canEdit),

            SizedBox(height: 16.h),

            // 花朵数量
            _buildBloomsSection(context),

            SizedBox(height: 16.h),

            // 病虫害信息
            _buildPestDiseaseSection(context, canEdit: canEdit),

            SizedBox(height: 30.h),

            // 操作按钮 - 只有可编辑的日期才显示
            if (canEdit) _buildActionButtons(context),

            SizedBox(height: 20.h),
          ],
        ),
      );
    });
  }

  /// 构建植物图片部分
  Widget _buildPlantImagesSection(BuildContext context, bool canEdit) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          'Plant Images',
          style: TextStyle(
            fontSize: 16.sp,
            fontWeight: FontWeight.w600,
            color: MyCommonUtils.white,
          ),
        ),

        SizedBox(height: 8.h),

        Obx(() {
          final images = controller.selectedImages;
          return GestureDetector(
            onTap: canEdit ? () => controller.pickImages(context) : null,
            child: AspectRatio(
              aspectRatio: 318 / 164,
              child: Container(
                width: double.infinity,
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(14.r),
                ),
                child:
                    images.isEmpty
                        ? Center(
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Icon(
                                Icons.image,
                                size: 40.sp,
                                color: MyCommonUtils.grey,
                              ),
                              SizedBox(height: 8.h),
                              Text(
                                'Tap to add image',
                                style: TextStyle(
                                  fontSize: 14.sp,
                                  color: MyCommonUtils.grey,
                                ),
                              ),
                            ],
                          ),
                        )
                        : Stack(
                          children: [
                            // 单张图片填充满整个容器
                            Container(
                              width: double.infinity,
                              height: double.infinity,
                              decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(14.r),
                                image: DecorationImage(
                                  image: FileImage(images.first),
                                  fit: BoxFit.cover,
                                  onError: (exception, stackTrace) {},
                                ),
                              ),
                            ),
                            // 删除按钮
                            if (canEdit)
                              Positioned(
                                top: 8.h,
                                right: 8.w,
                                child: GestureDetector(
                                  onTap: () => controller.removeImage(0),
                                  child: Container(
                                    width: 24.w,
                                    height: 24.w,
                                    decoration: BoxDecoration(
                                      color: Colors.red,
                                      shape: BoxShape.circle,
                                    ),
                                    child: Icon(
                                      Icons.close,
                                      color: Colors.white,
                                      size: 16.sp,
                                    ),
                                  ),
                                ),
                              ),
                          ],
                        ),
              ),
            ),
          );
        }),
      ],
    );
  }

  /// 构建叶片数量部分
  Widget _buildBladesSection(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          'Number of blades',
          style: TextStyle(
            fontSize: 16.sp,
            fontWeight: FontWeight.w600,
            color: MyCommonUtils.white,
          ),
        ),

        SizedBox(height: 8.h),

        _buildInputField(
          'Total blades',
          'Enter the total number of blades.',
          controller.totalBladesController,
          isNumber: true,
        ),

        SizedBox(height: 12.h),

        Row(
          children: [
            Expanded(
              child: _buildInputField(
                'Healthy leaves',
                'Number of healthy leaves',
                controller.healthyLeavesController,
                isNumber: true,
              ),
            ),

            SizedBox(width: 12.w),

            Expanded(
              child: _buildInputField(
                'Yellowed leaves',
                'Number of yellowed leaves',
                controller.yellowedLeavesController,
                isNumber: true,
              ),
            ),
          ],
        ),
      ],
    );
  }

  /// 构建浇水信息部分
  Widget _buildWateringSection(BuildContext context, {bool canEdit = true}) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          'Watering Information',
          style: TextStyle(
            fontSize: 16.sp,
            fontWeight: FontWeight.w600,
            color: MyCommonUtils.white,
          ),
        ),

        SizedBox(height: 8.h),

        _buildInputField(
          'Total watering volume',
          'Enter watering volume in ml',
          controller.wateringVolumeController,
          isNumber: true,
        ),

        SizedBox(height: 12.h),

        _buildCustomDropdownField(
          'Watering Method',
          controller.wateringMethodController,
          controller.wateringMethodOptions,
          controller.onWateringMethodSelected,
          readOnly: !canEdit,
          initialValue: controller.selectedWateringMethod.value?.displayName,
        ),
      ],
    );
  }

  /// 构建施肥信息部分
  Widget _buildFertilizationSection(
    BuildContext context, {
    bool canEdit = true,
  }) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          'Fertilization Information',
          style: TextStyle(
            fontSize: 16.sp,
            fontWeight: FontWeight.w600,
            color: MyCommonUtils.white,
          ),
        ),

        SizedBox(height: 8.h),

        _buildInputField(
          'Application rate',
          'Enter application rate',
          controller.applicationRateController,
          isNumber: true,
        ),

        SizedBox(height: 12.h),

        _buildCustomDropdownField(
          'Fertilizer Type',
          controller.fertilizerTypeController,
          controller.fertilizerTypeOptions,
          controller.onFertilizerTypeSelected,
          readOnly: !canEdit,
          initialValue: controller.selectedFertilizerType.value?.displayName,
        ),

        SizedBox(height: 12.h),

        _buildCustomDropdownField(
          'Application Method',
          controller.applicationMethodController,
          controller.applicationMethodOptions,
          controller.onApplicationMethodSelected,
          readOnly: !canEdit,
          initialValue: controller.selectedApplicationMethod.value?.displayName,
        ),
      ],
    );
  }

  /// 构建花朵数量部分
  Widget _buildBloomsSection(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          'Number of blooms',
          style: TextStyle(
            fontSize: 16.sp,
            fontWeight: FontWeight.w600,
            color: MyCommonUtils.white,
          ),
        ),

        SizedBox(height: 8.h),

        _buildInputField(
          'Blooming flowers',
          'Number of blooming flowers',
          controller.totalBloomsController,
          isNumber: true,
        ),

        SizedBox(height: 12.h),

        Row(
          children: [
            Expanded(
              child: _buildInputField(
                'Half-open flowers',
                'Number of half-open flowers',
                controller.halfOpenFlowersController,
                isNumber: true,
              ),
            ),

            SizedBox(width: 12.w),

            Expanded(
              child: _buildInputField(
                'Wilted flowers',
                'Number of wilted flowers',
                controller.wiltedFlowersController,
                isNumber: true,
              ),
            ),
          ],
        ),
      ],
    );
  }

  /// 构建病虫害信息部分
  Widget _buildPestDiseaseSection(BuildContext context, {bool canEdit = true}) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          'Types of Pests and Diseases',
          style: TextStyle(
            fontSize: 16.sp,
            fontWeight: FontWeight.w600,
            color: MyCommonUtils.white,
          ),
        ),

        SizedBox(height: 8.h),

        _buildCustomDropdownField(
          'Specific type',
          controller.pestDiseaseTypeController,
          controller.pestDiseaseTypeOptions,
          controller.onPestDiseaseTypeSelected,
          readOnly: !canEdit,
          initialValue: controller.selectedPestDiseaseType.value?.displayName,
        ),

        SizedBox(height: 12.h),

        _buildCustomDropdownField(
          'Site of onset',
          controller.onsetSiteController,
          controller.onsetSiteOptions,
          controller.onOnsetSiteSelected,
          readOnly: !canEdit,
          initialValue: controller.selectedOnsetSite.value?.displayName,
        ),
      ],
    );
  }

  /// 构建输入字段
  Widget _buildInputField(
    String label,
    String hint,
    TextEditingController controller, {
    bool isNumber = false,
    bool readOnly = false,
  }) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          label,
          style: TextStyle(
            fontSize: 14.sp,
            fontWeight: FontWeight.w600,
            color: MyCommonUtils.white,
          ),
        ),

        SizedBox(height: 4.h),

        TextField(
          controller: controller,
          keyboardType: isNumber ? TextInputType.number : TextInputType.text,
          style: TextStyle(fontSize: 14.sp),
          readOnly: readOnly,
          decoration: InputDecoration(
            hintText: hint,
            hintStyle: TextStyle(fontSize: 14.sp, color: MyCommonUtils.grey),
            filled: true,
            fillColor: readOnly ? Colors.grey.shade200 : Colors.white,
            border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(100.r),
              borderSide: BorderSide.none,
            ),
            contentPadding: EdgeInsets.symmetric(
              horizontal: 12.w,
              vertical: 8.h,
            ),
          ),
        ),
      ],
    );
  }

  /// 构建自定义下拉选择字段
  Widget _buildCustomDropdownField(
    String label,
    TextEditingController controller,
    List<String> items,
    Function(String) onSelected, {
    bool readOnly = false,
    String? initialValue,
  }) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          label,
          style: TextStyle(
            fontSize: 14.sp,
            fontWeight: FontWeight.w600,
            color: MyCommonUtils.white,
          ),
        ),

        SizedBox(height: 4.h),

        readOnly
            ? Container(
              padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 8.h),
              decoration: BoxDecoration(
                color: Colors.grey.shade200,
                borderRadius: BorderRadius.circular(20.r),
              ),
              child: Text(
                initialValue ?? 'No selection',
                style: TextStyle(fontSize: 14.sp, color: MyCommonUtils.grey),
              ),
            )
            : CustomDropdown<String>(
              key: ValueKey(initialValue ?? 'empty'),
              hintText: 'Select option',
              items: items,
              controller: SingleSelectController<String?>(initialValue),
              onChanged: (value) {
                if (value != null) {
                  onSelected(value);
                }
              },
              decoration: CustomDropdownDecoration(
                closedBorder: Border.all(color: Colors.grey.shade300),
                closedFillColor: Colors.white,
                expandedBorder: Border.all(
                  color: MyCommonUtils.orange,
                  width: 2.w,
                ),
                expandedFillColor: MyCommonUtils.orange,
                hintStyle: TextStyle(
                  fontSize: 14.sp,
                  color: MyCommonUtils.grey,
                ),
                listItemStyle: TextStyle(
                  fontSize: 14.sp,
                  color: MyCommonUtils.white,
                  fontWeight: FontWeight.w500,
                ),
                closedBorderRadius: BorderRadius.circular(20.r),
                expandedBorderRadius: BorderRadius.only(
                  bottomLeft: Radius.circular(20.r),
                  bottomRight: Radius.circular(20.r),
                ),
              ),
            ),
      ],
    );
  }

  /// 构建操作按钮
  Widget _buildActionButtons(BuildContext context) {
    return Row(
      children: [
        Expanded(
          child: ElevatedButton(
            onPressed: () => controller.clearForm(),
            style: ElevatedButton.styleFrom(
              backgroundColor: Colors.white,
              foregroundColor: MyCommonUtils.black,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(14.r),
              ),
              padding: EdgeInsets.symmetric(vertical: 12.h),
            ),
            child: Text(
              'Clear',
              style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.w600),
            ),
          ),
        ),

        SizedBox(width: 16.w),

        Expanded(
          child: ElevatedButton(
            onPressed: () => controller.saveRecord(context),
            style: ElevatedButton.styleFrom(
              backgroundColor: MyCommonUtils.green,
              foregroundColor: Colors.white,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(14.r),
              ),
              padding: EdgeInsets.symmetric(vertical: 12.h),
            ),
            child: Text(
              'Save',
              style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.w600),
            ),
          ),
        ),
      ],
    );
  }
}
